package com.guomei.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guomei.bean.Feedback;
import com.guomei.mapper.FeedbackMapper;
import com.guomei.service.FeedbackService;
import com.guomei.utils.PageData;
import com.guomei.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
@Transactional
public class FeedbackServiceImpl implements FeedbackService {
    @Autowired
    private FeedbackMapper feedbackMapper;

    @Override
    public Result<?> submitFeedback(Feedback feedback) {
        try {
            if (feedback == null) {
                return Result.fail("反馈信息不能为空");
            }

//            if (feedback.getTitle() == null || feedback.getTitle().trim().isEmpty()) {
//                return Result.fail("反馈标题不能为空");
//            }

            if (feedback.getContent() == null || feedback.getContent().trim().isEmpty()) {
                return Result.fail("反馈内容不能为空");
            }

            if (feedback.getFeedbackName() == null || feedback.getFeedbackName().trim().isEmpty()) {
                return Result.fail("反馈名称不能为空");
            }
            if (feedback.getUserPhone() == null || feedback.getUserPhone().trim().isEmpty()) {
                return Result.fail("手机号不能为空");
            }

//            if (feedback.getFeedbackType() == null || feedback.getFeedbackType().trim().isEmpty()) {
//                return Result.fail("反馈类型不能为空");
//            }

            int result = feedbackMapper.insert(feedback);
            if (result > 0) {
                log.info("用户提交反馈成功，反馈ID: {}", feedback.getFeedbackId());
                return Result.success("反馈提交成功", feedback.getFeedbackId());
            } else {
                return Result.fail("反馈提交失败");
            }
        } catch (Exception e) {
            log.error("提交反馈时发生异常", e);
            return Result.fail("提交反馈失败: " + e.getMessage());
        }
    }

    @Override
    public Result<Feedback> getFeedbackById(Long feedbackId) {
        try {
            if (feedbackId == null) {
                return Result.fail("反馈ID不能为空");
            }

            Feedback feedback = feedbackMapper.selectById(feedbackId);
            if (feedback == null) {
                return Result.fail("未找到指定的反馈记录");
            }

            return Result.success(feedback);
        } catch (Exception e) {
            log.error("查询反馈详情时发生异常，反馈ID: {}", feedbackId, e);
            return Result.fail("查询反馈详情失败: " + e.getMessage());
        }
    }

    @Override
    public Result<PageData<Feedback>> getFeedbackList(Feedback feedback, int pageNum, int pageSize) {
        try {
            PageHelper.startPage(pageNum, pageSize);
            List<Feedback> feedbackList = feedbackMapper.selectList(feedback);

            PageInfo<Feedback> pageInfo = new PageInfo<>(feedbackList);
            PageData<Feedback> pageData = new PageData<>();
            pageData.setTotal(pageInfo.getTotal());
            pageData.setData(pageInfo.getList());
            pageData.setPageNum(pageNum);
            pageData.setPageSize(pageSize);

            return Result.success(pageData);
        } catch (Exception e) {
            log.error("查询反馈列表时发生异常", e);
            return Result.fail("查询反馈列表失败: " + e.getMessage());
        }
    }

    @Override
    public Result<?> updateFeedbackStatus(Feedback feedback) {
        try {
            if (feedback.getFeedbackId() == null) {
                return Result.fail("反馈ID不能为空");
            }

            int result = feedbackMapper.updateStatus(feedback);
            if (result > 0) {
                log.info("更新反馈状态成功，反馈ID: {}", feedback.getFeedbackId());
                return Result.success("更新反馈状态成功");
            } else {
                return Result.fail("更新反馈状态失败");
            }
        } catch (Exception e) {
            log.error("更新反馈状态时发生异常，反馈ID: {}", feedback.getFeedbackId(), e);
            return Result.fail("更新反馈状态失败: " + e.getMessage());
        }
    }

    @Override
    public Result<?> deleteFeedback(Long feedbackId) {
        try {
            if (feedbackId == null) {
                return Result.fail("反馈ID不能为空");
            }

            int result = feedbackMapper.deleteById(feedbackId);
            if (result > 0) {
                log.info("删除反馈记录成功，反馈ID: {}", feedbackId);
                return Result.success("删除反馈记录成功");
            } else {
                return Result.fail("删除反馈记录失败");
            }
        } catch (Exception e) {
            log.error("删除反馈记录时发生异常，反馈ID: {}", feedbackId, e);
            return Result.fail("删除反馈记录失败: " + e.getMessage());
        }
    }

    @Override
    public Result<?> getFeedbackStatistics() {
        try {
            Map<String, Object> statistics = new HashMap<>();

            // 统计各类状态的反馈数量
            Feedback statusQuery = new Feedback();

            statusQuery.setStatus("待处理");
            int pendingCount = feedbackMapper.count(statusQuery);

            statusQuery.setStatus("处理中");
            int processingCount = feedbackMapper.count(statusQuery);

            statusQuery.setStatus("已解决");
            int resolvedCount = feedbackMapper.count(statusQuery);

            statusQuery.setStatus("已拒绝");
            int rejectedCount = feedbackMapper.count(statusQuery);

            // 统计各类型的反馈数量
            Feedback typeQuery = new Feedback();
            typeQuery.setFeedbackType("功能建议");
            int suggestionCount = feedbackMapper.count(typeQuery);

            typeQuery.setFeedbackType("bug 反馈");
            int bugCount = feedbackMapper.count(typeQuery);

            typeQuery.setFeedbackType("体验优化");
            int experienceCount = feedbackMapper.count(typeQuery);

            statistics.put("pendingCount", pendingCount);
            statistics.put("processingCount", processingCount);
            statistics.put("resolvedCount", resolvedCount);
            statistics.put("rejectedCount", rejectedCount);
            statistics.put("suggestionCount", suggestionCount);
            statistics.put("bugCount", bugCount);
            statistics.put("experienceCount", experienceCount);

            return Result.success(statistics);
        } catch (Exception e) {
            log.error("获取反馈统计数据时发生异常", e);
            return Result.fail("获取反馈统计数据失败: " + e.getMessage());
        }
    }
}
